<?php

namespace App\Http\Controllers\Admin;
use App\Http\Util\Err;
use App\Models\ExportMessage;
use App\Models\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class ExcelController extends Controller
{
    //Excel文件导出功能

    public function exportMessage(){
        $post['data'] = request()->get('data');
        $post['data'] = json_decode($post['data'],true);
        $headings = ['编号','电话','姓名','邮箱','留言','时间'];
        $data = Message::whereIn('id',$post['data'])
        ->select('id','phone','name','email','content','created_at')
        ->orderBy('created_at','desc')->get()->toArray();
         return Excel::download(new ExportMessage($data, $headings), date('Y-m-d',time()).'members.xls');
    }


//    public function exportMember(){
//        $post = request()->post();
//        $cellData[] = ['id','登录名','微信名','手机','真实姓名','最后登录时间'];
//        $data = Members::whereIn('id',$post['data'])
//            ->select('id','login_name','wechat_name','mobile','true_name','last_login_time')
//            ->orderBy('last_login_time','desc')->get()->toArray();
//        foreach($data as $item){
//            $cellData[] =[$item['id'],
//                $item['login_name'],
//                $item['wechat_name'],
//                $item['mobile'],
//                $item['true_name'],
//                $item['last_login_time']
//                ];
//        }
//        Excel::create('学生成绩',function($excel) use ($cellData){
//            $excel->sheet('score', function($sheet) use ($cellData){
//                $sheet->rows($cellData);
//            });
//        })->export('xls');
//    }
}
